<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\datacache;
use think\facade\Cache;

/**
 * 视频评论 缓存层
 * @package app\user_center\model
 */
class VideoPlayClass{

    /**
     * 视频分类
     * @param play_id int 视频id
     * @param cache 是否更新 缓存
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getPlayClassNameByClassid($classid, $cache = false){
        $Info = model('video/VideoPlayClass') -> cache(60*60*24) -> find($classid);
        return $Info;
    }
	
	/**
     * 查询一级分类级 和 子分类 下是否有视频
     * @param pid int 分类id
     * @param cache 是否更新 缓存
     * @author shiqiang
     * @return array
     */
	public function getPlayClassListLevel1($pid = 0, $cache = true){
		$cacheName = 'get_play_class_list_level1';
		$cache_list = Cache::get($cacheName);//print_r(empty($cache_list));
        if(empty($cache_list) || $cache){
			$class_parents = model('video/VideoPlayClass') -> where('pid', $pid) -> order('sort asc,id asc') -> cache(60*60*24) -> column('id,title');//一级分类

			$class_level1 = [];
			foreach($class_parents as $class_pid => $value){
				$class_childs = model('video/VideoPlayClass') -> getChildsInfo($class_pid);//子集
				$class_childs[$class_pid] = $value;// 父节点 + 子节点 集合

				$class_list = [];
				foreach($class_childs as $video_classid => $val){//获取分类下是否有视频
					$play_info = datacache('VideoPlay', 'datacache') -> getClassPlayPushList($video_classid, $play_type = 1);//查询视频
                    if(!empty($play_info['data'])){
						$class_list[] = ['id' => $video_classid, 'title' => $value];//重新赋值
					}
				}
				if(!empty($class_list)) $class_level1[] = array('id' => $class_pid, 'title' => $value);
			}
			Cache::set($cacheName, $class_level1, 60*60*24);//默认缓存1天
		}
		return Cache::get($cacheName);
	}

}
